package dao;

import java.util.ArrayList;

import javax.sql.DataSource;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import dto.CompanyDto;
import dto.FileDto;

public class FileDaoImpl implements FileDao {
	private JdbcTemplate template;

	public void setDataSource(DataSource dataSource) {
		this.template = new JdbcTemplate(dataSource);
	}

	private static final String INSERT_FILE_INSERT = "INSERT INTO file(member_id, file_name, file_area, file_kind) "
			+ " VALUES(?,?,?,?)";

	@Override
	public int insertFile(FileDto bean) {
		// TODO Auto-generated method stub
		return this.template.update(
				INSERT_FILE_INSERT,
				new Object[] { bean.getMember_id(), bean.getFile_name(),
						bean.getFile_area(), bean.getFile_kind() });
	}

	// private static final String SELECT_FILE_LIST =
	// "SELECT * FROM file WHERE file_key = ?";
	private static final String SELECT_FILE_LIST = "SELECT pr_key, file_name, file_area, file_kind FROM file WHERE member_id = ?";

	@Override
	public ArrayList<FileDto> fileList(String member_id) {
		// TODO Auto-generated method stub
		RowMapper<FileDto> mapper = new BeanPropertyRowMapper<FileDto>(
				FileDto.class);
		ArrayList<FileDto> query = (ArrayList<FileDto>) template.query(
				SELECT_FILE_LIST, mapper, member_id);
		return query;
	}
	
	private static final String SELECT_HIT_LIST = "SELECT pr_key, s_date, e_date, name, kind, email, link FROM company WHERE kind = ?";

	@Override
	public ArrayList<CompanyDto> hitList(String kind) {
		// TODO Auto-generated method stub
		System.out.println("hit impl = " + kind);
		RowMapper<CompanyDto> mapper = new BeanPropertyRowMapper<CompanyDto>(
				CompanyDto.class);
		ArrayList<CompanyDto> query = (ArrayList<CompanyDto>) template.query(
				SELECT_HIT_LIST, mapper, kind);
		return query;
	}

	private static final String DELECT_FILE_REMOVE = "DELETE FROM file WHERE pr_key = ?";

	@Override
	public int removeFile(FileDto bean) {
		// TODO Auto-generated method stub
		return this.template.update(DELECT_FILE_REMOVE, bean.getPr_key());
	}

	@Override
	public int compareFile(String fileName, String loginId) {
		// TODO Auto-generated method stub
		try {
			System.out.println("쿼리문 왜 안들어감?");
			return this.template
					.queryForInt("SELECT pr_key FROM file WHERE member_id = '"
							+ loginId + "' && file_name = '" + fileName + "'");
		} catch (EmptyResultDataAccessException e) {
			return 0;
		}
	}

	private static final String SLELECT_FILE_ID_EMAIL = "SELECT * FROM file WHERE member_id = ?";

	@Override
	public FileDto selectOne(String id) {
		// TODO Auto-generated method stub
		RowMapper<FileDto> mapper = new BeanPropertyRowMapper<FileDto>(
				FileDto.class);
		try {
			return this.template.queryForObject(SLELECT_FILE_ID_EMAIL, mapper,
					id);
		} catch (Exception e) {
			return null;
		}
	}
}
